﻿using System;
using System.IO;

using System.Windows.Forms;
using System.Data.SqlClient;

namespace GenerateSignature
{
	public partial class FrmGenerateSig : Form
	{
		private RSASignature rsaSign = null;
		private string connectstr = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\\RSA_Signature.mdf;Integrated Security=True";

		public FrmGenerateSig()
		{
			InitializeComponent();

			// init 
			rsaSign = new RSASignature();
		}

		private void ConnectDB()
		{
			SqlConnection sqlconn = new SqlConnection();

			try
			{
				// MySQL Addapter

				sqlconn.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\\RSA_Signature.mdf;Integrated Security=True";
				sqlconn.Open();
				SqlCommand sqlCom = new SqlCommand("Select * from RSA_Signature");
				SqlDataReader dtReader = sqlCom.ExecuteReader();
			}
			catch (Exception ee)
			{
				Console.WriteLine(ee.ToString());
			}
		}

		private void btnGenerate_Click(object sender, EventArgs e)
		{
			rsaSign.MakeSig();

			//byte bi = 8;
			//rtbPrivateKey.Text += "[" + bi.ToString() + "]";

			//key = GenerateSignatue.Gen();

			//// Private key
			//rtbPrivateKey.Text += "D: \n\n";

			//foreach (byte bi in key.D)
			//{
			//	rtbPrivateKey.Text += bi.ToString();
			//}

			//// Public key
			//rtbPublicKey.Text += "Modulus (N) \n\n";
			//foreach (byte bi in key.Modulus)
			//{
			//	rtbPublicKey.Text += bi.ToString();
			//}

			//rtbPublicKey.Text += "\n\nExponent (E) \n\n";
			//foreach (byte bi in key.Exponent)
			//{
			//	rtbPublicKey.Text += bi.ToString();
			//}
		}


		public void SaveKey()
		{
			// Save to Local DB
			// connect to Loacal DB
			ConnectDB();

			// loop and save RSA key to Database
			foreach( byte bi in rsaSign.key.D)
			{
				// store procedure ?
				// 
			}

			//int count = 0;
			//// create xml data
			//// XDocument
			//// save xml data to file
			//XDocument xmlDoc = new XDocument();

			//XElement elementD = new XElement("D", key.D);
			//foreach (byte bi in key.D)
			//{
			//	count++;
			//	if (bi == 0) { }

			//	elementD.Add(new XElement(count.ToString(), bi));
			//}

			//// byte -> list -> xml ?

			//xmlDoc.Add(elementD);

			//SaveFileDialog saveKey = new SaveFileDialog();
			//DialogResult result = saveKey.ShowDialog();
			//if (DialogResult.OK == result)
			//{
			//	xmlDoc.Save(saveKey.FileName);
			//}
		}

		private void btnSaveKey_Click(object sender, EventArgs e)
		{
			SaveKey();
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileSign = new OpenFileDialog();
			openFileSign.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
			openFileSign.FilterIndex = 1;

			openFileSign.Multiselect = false;

			// Call the ShowDialog method to show the dialog box.
			DialogResult userClickedOK = openFileSign.ShowDialog();

			// Process input if the user clicked OK.
			if (userClickedOK == DialogResult.OK)
			{
				tbFileToSign.Text = openFileSign.FileName;
			}
		}

		private void btnSaveFile_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderDialog = new FolderBrowserDialog();
			DialogResult dlgResult = folderDialog.ShowDialog();
			
			if( DialogResult.OK == dlgResult)
			{
				tbSignedFile.Text = folderDialog.SelectedPath;
			}

		}

		private void btnSign_Click(object sender, EventArgs e)
		{
			rsaSign.SignFile(rsaSign.key, tbFileToSign.Text, tbSignedFile.Text);
		}

		private void FrmGenerateSig_Load(object sender, EventArgs e)
		{
			// TODO: This line of code loads data into the 'rSA_SignatureDataSet.RSA_Signature' table. You can move, or remove it, as needed.
			this.rSA_SignatureTableAdapter.Fill(this.rSA_SignatureDataSet.RSA_Signature);

		}
	}


	// CA ? => mysql database => store to verify 
	// Make signature?
	// Sign
	// Verify

}
